﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour {

    private IUserInput pi;
    public float horizontalSpeed = 100.0f;
    public float velocitySpeed = 100.0f;
    public float cameraDampValue = 0.05f;

    private GameObject playerHandle;
    private GameObject cameraHandle;
    private float tempEulerX;
    private GameObject model;
    private GameObject newCamera;

    private Vector3 cameraDampVelocity;
    // Use this for initialization
    void Start () {
        cameraHandle = transform.parent.gameObject;
        playerHandle = cameraHandle.transform.parent.gameObject;

        ActorController ac = playerHandle.GetComponent<ActorController>();
        pi = ac.pi;
        model = ac.model;
        newCamera = Camera.main.gameObject;

        tempEulerX = 20.0f;

        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void FixedUpdate () {
        Vector3 tempModelEuler = model.transform.eulerAngles;

        playerHandle.transform.Rotate(Vector3.up, pi.Jright * horizontalSpeed * Time.fixedDeltaTime);
        //cameraHandle.transform.Rotate(Vector3.right, pi.Jup * -velocitySpeed * Time.deltaTime);
        tempEulerX -= pi.Jup * velocitySpeed * Time.fixedDeltaTime;
        tempEulerX = Mathf.Clamp(tempEulerX, -40, 30);
        cameraHandle.transform.localEulerAngles = new Vector3(tempEulerX, 0, 0);

        model.transform.eulerAngles = tempModelEuler;

        //camera.transform.position = Vector3.Lerp(camera.transform.position, transform.position, 0.2f);
        newCamera.transform.position = Vector3.SmoothDamp(newCamera.transform.position, transform.position, ref cameraDampVelocity, cameraDampValue);
        //newCamera.transform.eulerAngles = transform.eulerAngles;
        newCamera.transform.LookAt(cameraHandle.transform);


    }
}
